<template>
    <div>
        <el-dialog :visible.sync="editDialogVisible" title="修改航班信息">
            <el-form :model="flight" :rules="rules" label-width="100px" style="width: 500px; margin-left: 180px" size="small " ref="flightForm">
                <el-form-item label="航班编号" prop="fliNo">
                    <el-input v-model="flight.fliNo" placeHolder=""/>
                </el-form-item>

                <el-form-item label="飞机编号" prop="airCode">
                    <el-select v-model="flight.airCode" placeholder="AC188" style="float: left;width: 400px">
                        <el-option
                                v-for="item in planeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="公司编号" prop="comCode">
                    <el-select v-model="flight.comCode" placeholder="毅泽航空" style="float: left;width: 400px">
                        <el-option
                                v-for="item in companyOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="起始地址" prop="fliBaddress">
                    <el-select v-model="flight.fliBaddress" filterable placeholder="请选择" style="float: left;width: 400px">
                        <el-option
                                v-for="itemB in cityOptions"
                                :key="itemB.value"
                                :label="itemB.label"
                                :value="itemB.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="到达地址" prop="fliAaddress">
                    <!--                这里有问题！不能和起始地址重复-->
                    <el-select v-model="flight.fliAaddress" filterable placeholder="请选择" style="float: left;width: 400px">
                        <el-option
                                v-for="itemA in cityOptions"
                                :key="itemA.value"
                                :label="itemA.label"
                                :value="itemA.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="起飞时间" prop="fliBtime">
                    <el-date-picker
                            value-format="yyyy-MM-dd HH:mm:ss"
                            v-model="flight.fliBtime"
                            type="datetime"
                            placeholder="选择日期时间"
                            style="float: left;width: 400px">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="到达时间" prop="fliAtime">
                    <el-date-picker
                            value-format="yyyy-MM-dd HH:mm:ss"
                            v-model="flight.fliAtime"
                            type="datetime"
                            placeholder="选择日期时间"
                            style="float: left;width: 400px">
                    </el-date-picker>
                </el-form-item>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="头等舱价格" prop="fliFfare">
                            <el-input v-model="flight.fliFfare"  placeHolder=""/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="剩余人数" prop="fliFnumber">
                            <el-input v-model="flight.fliFnumber" placeHolder=""/>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="公务舱价格" prop="fliCfare">
                            <el-input v-model="flight.fliCfare" placeHolder=""/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="剩余人数" prop="fliCnumber">
                            <el-input v-model="flight.fliCnumber" placeHolder=""/>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="经济舱价格" prop="fliYfare">
                            <el-input v-model="flight.fliYfare" placeHolder=""/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="剩余人数" prop="fliYnumber">
                            <el-input v-model="flight.fliYnumber" placeHolder=""/>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item>
                    <el-button type="primary" @click="update" :disabled="btnSts">确定修改</el-button>
                    <el-button @click="editDialogVisible=false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <el-breadcrumb separator-class="el-icon-arrow-right"
                       style="background-color:white;padding: 8px;margin-bottom: 20px">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>航班信息管理</el-breadcrumb-item>
            <el-breadcrumb-item>航班信息列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-table :data="flightInformationList" width="100%">
            <el-table-column prop="fliNo" label="航班编号"></el-table-column>
            <el-table-column prop="comCode" label="航空公司"></el-table-column>
            <el-table-column prop="fliBaddress" label="起始地"></el-table-column>
            <el-table-column prop="fliAaddress" label="目的地"></el-table-column>
            <el-table-column prop="fliBtime" label="起降时间" align="right"></el-table-column>
            <el-table-column prop="fliAtime" label=""></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="editInformation(scope.$index,scope.row)">编辑</el-button>
                    <el-button type="danger" size="mini" @click="deleteManager(scope.$index,scope.row)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>
        <el-pagination
                style="margin-top: 10px"
                background
                layout="prev, pager, next"
                :current-page.sync="pageInfo.pageNum"
                :page-count="pageInfo.pages"
                :page-size="pageInfo.pageSize"
                :total="pageInfo.total"
                @current-change="this.findList"
        >
        </el-pagination>

    </div>
</template>

<script>
    export default {
        name: "FlightInformationList",
        created() {
            this.findList();
        },
        methods: {
            /**
             * 获取表格内数据
             */
            findList() {
                axios.post("/flightinformation/page", {
                    pageNum:this.pageInfo.pageNum,
                    pageSize:this.pageInfo.pageSize,
                    condition:this.flight
                }).then(resp => {
                    //console.log(resp.data);
                    this.pageInfo.pageSize=resp.data.pageSize;
                    this.pageInfo.pageNum=resp.data.pageNum;
                    this.pageInfo.total=resp.data.total;
                    this.pageInfo.pages=resp.data.pages;
                    //将数据赋值给managerList表格数据
                    this.flightInformationList=resp.data.list;
                })
            },
            deleteManager(index, row) {
                this.$confirm('此操作将永久删除该航班信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送请求去删除对应的数据
                    axios.delete('/flightinformation/delete/'+row.id).then(resp=>{
                        if (resp.data==='success'){
                            this.$message({
                                type:'success',
                                message:'删除成功!'
                            })
                        }else {
                            this.$message({
                                type:'error',
                                message:'删除失败!'
                            })
                        }
                        //重新获取表格数据
                        this.findList();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            editInformation(index, row) {
                //记录下原来的用户名
                this.vaNo=row.fliNo;
                this.flight = row;
                this.editDialogVisible = true;
            },
            update() {
                //将提交按钮设置为不可用状态 按钮动态加disable属性
                this.btnSts = true;
                //1、先验证表单格式没问题了，再允许发送请求
                this.$refs['flightForm'].validate((valid) => {
                    if (valid) {
                        let  payLoad=this.flight;
                        // 进行表单的提交
                        axios.post("/flight/save", payLoad).then(response => {
                            this.btnSts = false;
                            if (response.data === 'success') {
                                this.$notify({
                                    title: "提示",
                                    message: "用户保存成功",
                                    type: "success"
                                });
                                //一旦提交成功，自动关闭窗口
                                this.editDialogVisible = false;
                                //刷新表格数据
                                this.findList();
                            }
                        })
                    } else {
                        this.btnSts = false;
                        return false;
                    }
                })
            }
        },
            data() {
                //航班编号不能为空，且长度在3-10之间
                let validFliNo = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("航班编号不能为空"));
                    }
                    if (value.length < 3 || value.length > 10) {
                        return callback(new Error("航班编号长度3-10之间"));
                    }
                    return callback();
                };
                //飞机编号不能为空
                let validAirCode = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("飞机编号不能为空"));
                    }
                    return callback();
                };
                //公司编号不能为空
                let validComCode = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("公司编号不能为空"));
                    }
                    return callback();
                };
                //起始地址不能为空
                let validFliBadDress = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("起始地址不能为空"));
                    }
                    return callback();
                };
                //到达地址不能为空，且不能和起始地址相同
                let validFliAadDress = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("到达地址不能为空"));
                    }
                    if(value===this.flight.fliBaddress){
                        return callback(new Error("到达地址不能和起始地址相同！"));
                    }
                    return callback();
                };

                //起飞时间不能为空，且大于当前时间
                let validFliBtime = (rule, value, callback) => {
                    Date.prototype.Format = function (fmt) {
                        var o = {
                            "M+": this.getMonth() + 1, // 月份
                            "d+": this.getDate(), // 日
                            "h+": this.getHours(), // 小时
                            "m+": this.getMinutes(), // 分
                            "s+": this.getSeconds(), // 秒
                            "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
                            "S": this.getMilliseconds() // 毫秒
                        };
                        if (/(y+)/.test(fmt))
                            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + ""));
                        for (var k in o)
                            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                        return fmt;
                    }
                    if (!value || value === '') {
                        return callback(new Error("起飞时间不能为空"));
                    }
                    if(value<(new Date(new Date().getTime()).Format('yy-MM-dd hh:mm:ss'))){
                        return callback(new Error("起飞时间应大于当前时间"));
                    }
                    return callback();
                };
                //到达时间不能为空，且大于起飞时间
                let validFliAtime = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("到达时间不能为空"));
                    }
                    if(value<this.flight.fliBtime){
                        return callback(new Error("到达时间不能小于起飞时间！"));
                    }
                    return callback();
                };
                //头等舱价格不能为空，正则：不能以0开头，小数点最多两位
                let validFliFfare = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("头等舱价格不能为空"));
                    }
                    let pattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                    if (!pattern.test(value)) {
                        return callback(new Error("格式错误！请重新输入"));
                    }
                    return callback();
                };
                //公务舱价格不能为空，正则：不能以0开头，小数点最多两位
                let validFliCfare = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("公务舱价格不能为空"));
                    }
                    let pattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                    if (!pattern.test(value)) {
                        return callback(new Error("格式错误！请重新输入"));
                    }
                    return callback();
                };
                //经济舱价格不能为空，正则：不能以0开头，小数点最多两位
                let validFliYfare = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("经济舱价格不能为空"));
                    }
                    let pattern = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
                    if (!pattern.test(value)) {
                        return callback(new Error("格式错误！请重新输入"));
                    }
                    return callback();
                };
                //头等舱人数不能为空，正则：非负整数
                let validFliFnumber = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("头等舱价格不能为空"));
                    }
                    let pattern = /^[1-9]\d*|0$/;
                    if (!pattern.test(value)) {
                        return callback(new Error("格式错误！请重新输入"));
                    }
                    return callback();
                };
                //公务舱人数不能为空，正则：非负整数
                let validFliCnumber = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("公务舱价格不能为空"));
                    }
                    let pattern = /^[1-9]\d*|0$/;
                    if (!pattern.test(value)) {
                        return callback(new Error("格式错误！请重新输入"));
                    }
                    return callback();
                };
                //经济舱人数不能为空，正则：非负整数
                let validFliYnumber = (rule, value, callback) => {
                    if (!value || value === '') {
                        return callback(new Error("经济价格不能为空"));
                    }
                    let pattern = /^[1-9]\d*|0$/;
                    if (!pattern.test(value)) {
                        return callback(new Error("格式错误！请重新输入"));
                    }
                    return callback();
                };

            return {
                //pageInfo对象实际是从后台查询得到的，代表了分页必要的信息
                pageInfo:{
                    pageNum:1, /*当前是那一页*/
                    pageSize:5, /*每页展示多少条*/
                    pages:1, /*总共有多少页*/
                    total:100 /*总共有多少条数据*/
                },
                editDialogVisible: false,
                flight: {
                    fliNo: "",          //航班编号
                    airCode: "",        //飞机编号
                    comCode: "",        //公司编号
                    fliBaddress: '',    //起始地址
                    fliAaddress: '',     //到达地址
                    fliBtime: '',        //起飞时间
                    fliAtime: '',        //到达时间
                    fliFfare: '',       //头等舱价格
                    fliCfare: '',       //公务舱价格
                    fliYfare: '',       //经济舱价格
                    fliFnumber: '',     //头等舱剩余人数
                    fliCnumber: '',     //公务舱剩余人数
                    fliYnumber: '',     //经济舱剩余人数
                    flag: 0,
                    fliDiscount: 0,     //折扣
                    fliRefundtime: 0,  //退票记录时间
                },
                btnSts: false,
                flightInformationList: [],
                //记录原来的航班号
                vaNo:'',
                rules: {
                    fliNo: [
                        {validator: validFliNo, trigger: 'blur'}
                    ],
                    airCode: [
                        {validator: validAirCode, trigger: 'change'}
                    ],
                    comCode: [
                        {validator: validComCode, trigger: 'change'}
                    ],
                    fliBaddress: [
                        {validator: validFliBadDress, trigger: 'change'}
                    ],
                    fliAaddress: [
                        {validator: validFliAadDress, trigger: 'change'}
                    ],
                    fliBtime: [
                        {validator: validFliBtime, trigger: 'change'}
                    ],
                    fliAtime: [
                        {validator: validFliAtime, trigger: 'change'}
                    ],
                    fliFfare: [
                        {validator: validFliFfare, trigger: 'blur'}
                    ],
                    fliCfare: [
                        {validator: validFliCfare, trigger: 'blur'}
                    ],
                    fliYfare: [
                        {validator: validFliYfare, trigger: 'blur'}
                    ],
                    fliFnumber: [
                        {validator: validFliFnumber, trigger: 'blur'}
                    ],
                    fliCnumber: [
                        {validator: validFliCnumber, trigger: 'blur'}
                    ],
                    fliYnumber: [
                        {validator: validFliYnumber, trigger: 'blur'}
                    ],
                    // manAccount: [
                    //     {required: true, message: '用户名不能为空', trigger: 'blur'},
                    //     {min: 6, max: 20, message: '用户名6-20之间', trigger: 'blur'},
                    //     {validator: validAccount, trigger: 'blur'}
                    // ],
                    // checkPass: [
                    //     {validator: validPwd2, trigger: 'blur'}
                    // ],
                    // manId: [
                    //     {validator: validId, trigger: "blur"}
                    // ],
                    // manSex: [
                    //     {required: true, message: "请选择性别", trigger: "blur"}
                    // ],
                    // manTelNumber: [
                    //     {validator: validPhone, trigger: "blur"}
                    // ],
                    // manEmail: [
                    //     {validator: validEmail, trigger: "blur"}
                    // ]
                },
                planeOptions:"" ,
                // value: '',
                companyOptions: [{
                    value: '嘉晨航空',
                    label: '嘉晨航空'
                }, {
                    value: '新程航空',
                    label: '新程航空'
                }, {
                    value: '振宇航空',
                    label: '振宇航空'
                }, {
                    value: '毅泽航空',
                    label: '毅泽航空'
                }, {
                    value: '李达航空',
                    label: '李达航空'
                }, {
                    value: '国强航空',
                    label: '国强航空'
                }],
                cityOptions: [{
                    value: '北京',
                    label: '北京'
                }, {
                    value: '上海',
                    label: '上海'
                }, {
                    value: '广州',
                    label: '广州'
                }, {
                    value: '深圳',
                    label: '深圳'
                }, {
                    value: '苏州',
                    label: '苏州'
                }, {
                    value: '成都',
                    label: '成都'
                }, {
                    value: '天津',
                    label: '天津'
                }, {
                    value: '武汉',
                    label: '武汉'
                }, {
                    value: '南京',
                    label: '南京'
                }, {
                    value: '长沙',
                    label: '长沙'
                }, {
                    value: '郑州',
                    label: '郑州'
                }, {
                    value: '青岛',
                    label: '青岛'
                }, {
                    value: '无锡',
                    label: '无锡'
                }, {
                    value: '大连',
                    label: '大连'
                }, {
                    value: '西安',
                    label: '西安'
                }, {
                    value: '宁波',
                    label: '宁波'
                }, {
                    value: '济南',
                    label: '济南'
                }, {
                    value: '佛山',
                    label: '佛山'
                },{
                    value: '重庆',
                    label: '重庆'
                }]
            }
        }
    }
</script>

<style scoped>

</style>